package com.sky.controller.user;

import com.sky.constant.MessageConstant;
import com.sky.context.BaseContext;
import com.sky.pojo.AddressBook;
import com.sky.result.Result;
import com.sky.service.user.AddressBookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author zgp
 * @version V1.0
 * @date 2025-01-27 18:27
 */
@Slf4j
@Api(tags = "用户地址薄管理")
@RestController
@RequestMapping("/user/addressBook")
public class AddressBookController {
    @Autowired
    private AddressBookService addressBookService;

    @ApiOperation("查询当前用户所有地址信息")
    @GetMapping("/list")
    public Result<List<AddressBook>> getAll(){
        log.info("查询当前用户所有地址信息，userId = {}", BaseContext.getCurrentId());
        return Result.success(addressBookService.getAllAddressBook());
    }

    @ApiOperation("新增地址")
    @PostMapping
    public  Result<String> saveAddressBook(@RequestBody AddressBook addressBook){
        log.info("新增地址，userId = {}，addressBook = {}", BaseContext.getCurrentId(), addressBook);
        addressBookService.saveAddressBook(addressBook);
        return Result.success(MessageConstant.ADD_SUCCESS);
    }

    @ApiOperation("查询默认地址")
    @GetMapping("/default")
    public Result<AddressBook> getDefaultAddress(){
        log.info("查询默认地址，userId = {}", BaseContext.getCurrentId());
        return Result.success(addressBookService.getDefaultAddress());
    }

    @ApiOperation("设置默认地址")
    @PutMapping("/default")
    public Result<String> setDefaultAddress(@RequestBody AddressBook addressBook){
        log.info("设置默认地址，userId = {}，addressBook = {}", BaseContext.getCurrentId(), addressBook);
        addressBookService.setDefaultAddress(addressBook);
        return Result.success(MessageConstant.UPDATE_SUCCESS);
    }

    @ApiOperation("根据id查询地址")
    @GetMapping("/{id}")
    public Result<AddressBook> getAddressById(@PathVariable Long id){
        log.info("根据id查询地址，id：{}", id);
        return Result.success(addressBookService.getAddressById(id));
    }

    @ApiOperation("根据id删除地址")
    @DeleteMapping
    public Result<String> deleteAddressById(Long id){
        log.info("根据id删除地址，id：{}", id);
        addressBookService.deleteAddressById(id);
        return Result.success(MessageConstant.DELETE_SUCCESS);
    }

    @ApiOperation("修改地址信息")
    @PutMapping
    public Result<String> updateAddress(@RequestBody AddressBook addressBook){
        log.info("修改地址信息，addressBook：{}", addressBook);
        addressBookService.updateAddress(addressBook);
        return Result.success();
    }
}
